package com.crealth.db;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.crealth.manager.MybatisManager;
import com.crealth.model.CdrModel;
import com.crealth.utils.StringUtil;
import com.crealth.utils.Utils;

import java.sql.Timestamp;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.log4j.Logger;


public class CdrDb {
	private static Logger errorLog = Logger.getLogger("error_log");
	
	public static void createCdrTable() {
		String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
		try {
			MybatisManager.cdrMapper.createCdrTable(tbName);
			MybatisManager.commit();
		}
		catch (PersistenceException e) {
			errorLog.error("", e);
		}
	}
	
	public static void insertCdr(CdrModel cdrModel) {
		int userID = cdrModel.getUserID();
		String callMobi = cdrModel.getCallee();
		String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
		MybatisManager.cdrMapper.insertCdrObj(tbName, 
				userID, callMobi, 
				cdrModel.getHoldTm(), cdrModel.getEndreason(),
				cdrModel.getRecordName(),
				cdrModel.getDialType());
		MybatisManager.commit();
		
		ClienteleDb.updateSelfAlreadyCall(userID, callMobi, 1);
		
		long id = MybatisManager.cdrMapper.selectLastCdrID(tbName);
		cdrModel.setId(id);
	}
	
	public static Long selectUserCdrExit(int userID, String callee) {
		String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
		Long res =MybatisManager.cdrMapper.selectUserCdrExit(tbName, userID, callee);
		
		return res;
	}
	
	public static void updateCdrCallendByID(CdrModel cdrModel) {
		String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
		try {
			MybatisManager.cdrMapper.updateCdrCallendByID(tbName, 
					cdrModel.getId(), cdrModel.getUserID(), cdrModel.getHoldTm(), 
					cdrModel.getRingingTime(),
					cdrModel.getEndreason(), cdrModel.getRecordName());
			MybatisManager.commit();
		}
		catch (Exception e) {
			errorLog.error("", e);
		}
	}
	
	public static List<CdrModel> selectRecordCdr(long seaCallTm, String strSql, String searchPhoneNum) {
		Date date = null;
		if (seaCallTm > 0) {
			date = new Date(seaCallTm*1000);
		}
		else {
			date = new Date(System.currentTimeMillis());
		}
        String simpleFmt = new SimpleDateFormat("yyyyMMdd").format(date);
		String tbName = "e_cdr_" + simpleFmt.substring(0, 6);		
		try {
			List<CdrModel> cdrls = MybatisManager.cdrMapper.selectRecordCdr(tbName, strSql);
			
			if (seaCallTm == 0) {
				for (int selMonNum = -1; selMonNum >= -6; --selMonNum) {
				Date preDate = Utils.getPreMonthDate(date, selMonNum);
				simpleFmt = new SimpleDateFormat("yyyyMMdd").format(preDate);		
				tbName = "e_cdr_" + simpleFmt.substring(0, 6);
				if (existTable(tbName)) {
					List<CdrModel> preMonthCdrls = MybatisManager.cdrMapper.selectRecordCdr(tbName, strSql);
						if (preMonthCdrls != null && preMonthCdrls.size() > 0) {
							for (CdrModel curCdr : preMonthCdrls) {
								cdrls.add(curCdr);
							}
						}
					}
				}
			}
			
			return cdrls;
		}
		catch (Exception e) {
			errorLog.error("", e);
			return null;
		}
	}
	
    public static Integer selectUserOnedayCallSum(int userID) {
    	String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
    	Integer sum = MybatisManager.cdrMapper.selectUserOnedayCallSum(tbName, userID);
    	if (sum == null) {
    		sum = 0;
    	}
    	return sum;
    }
    
    public static Integer selectUserHoldCallSum(int userID) {
    	String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
    	Integer sum = MybatisManager.cdrMapper.selectUserHoldCallSum(tbName, userID);
    	if (sum == null) {
    		sum = 0;
    	}
    	return sum;
    }
    
    public static Integer selectUserRingCallSum(int userID) {
    	String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
    	Integer sum = MybatisManager.cdrMapper.selectUserRingCallSum(tbName, userID);
    	if (sum == null) {
    		sum = 0;
    	}
    	return sum;
    }
    
    public static Integer selectUserHoldTimeSum(int userID) {
    	String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
    	Integer sum = MybatisManager.cdrMapper.selectUserHoldTimeSum(tbName, userID);
    	if (sum == null) {
    		sum = 0;
    	}
    	return sum;
    }
    
    public static Integer selectGreaterThanOneminute(int userID) {
    	String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
    	Integer sum = MybatisManager.cdrMapper.selectGreaterThanOneminute(tbName, userID);
    	if (sum == null) {
    		sum = 0;
    	}
    	return sum;
    }
    
    
    
    public static List<CdrModel> selectAllCdr(String tbName) {
    	try {
    		List<CdrModel> cdrls = MybatisManager.cdrMapper.selectAllCdr(tbName);
    		return cdrls;
    	}
    	catch (PersistenceException e) {
    		return null;
    	}
    	
    }
    
    public static void insertCdrAllField(int userID, String callee,
    		int holdTm, String endreason, Timestamp callTime) {
    	String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
    	MybatisManager.cdrMapper.insertCdrAllField(tbName, userID, callee, holdTm, endreason, callTime);
    	MybatisManager.commit();
    }
    
    public static List<String> selectAllCdrCalleeByUserID(String tableName, int userID) {
    	List<String> calleels = MybatisManager.cdrMapper.selectAllCdrCalleeByUserID(tableName, userID);
    	
    	return calleels;
    }
    
    public static Integer selectCdrByPhoneAndTm(String phoneNum, Timestamp uploadTm) {
    	String tbName = "e_cdr_" + StringUtil.getCurrentDay().substring(0, 6);
    	Integer res = MybatisManager.cdrMapper.selectCdrByPhoneAndTm(tbName, phoneNum, uploadTm);
    	
    	return res;
    }
    
    public static boolean existTable(String tableName) {
    	if (tableName == null || tableName.length() == 0) {
    		return false;
    	}
    	
    	Integer cnt = MybatisManager.cdrMapper.selectTableCount(tableName);
    	if (cnt != null && cnt > 0) {
    		return true;
    	}
    	
    	return false;
    }
	
}
